<?php
class ReserveAction extends BaseAction {
	
	//留言板首页
	//用途：显示
	public function index() {
		$page = $this->_get("page");
		$page = $page > 1 ? $page : 1; //如page参数为[字母]或[为空]，情况的默认赋值
		$cid = $this->_get("cid");
		
		$catalog = CatalogHelper::getObject("catalog_id={$cid}");
		$reserveModule = ReserveModuleHelper::getObject("reserve_module_id=".$catalog["dynamic_module_id"]);
		$tableName = C("DB_PREFIX").$reserveModule["table_name"];
		
		$sql = "SELECT * FROM {$tableName} WHERE catalog_id={$cid} ORDER BY timeline DESC";
		$pagination = CommonUtil::getPage($sql,$page,10);
		foreach($pagination ["resultList"] as &$obj){
			$obj["timeline"] = CommonUtil::formatDate($obj["timeline"]);
		}
		$reserve_module_field = ReserveModuleFieldHelper::getList("reserve_module_id={$catalog["dynamic_module_id"]} AND is_system=0","order_num DESC",2);
		
		$this->assign("pagination",$pagination);
		$this->assign("reserve_module_field",$reserve_module_field);
		$this->display();
                
	}
	
	//详情
	//用途：显示
	public function detail() {
		$catalogId = $this->_get("cid");
		$id = $this->_get("id");
		
		$catalog = CatalogHelper::getObject("catalog_id={$catalogId}");
		$reserveModule = ReserveModuleHelper::getObject("reserve_module_id={$catalog["dynamic_module_id"]}");
		$tableName = C("DB_PREFIX").$reserveModule["table_name"];
		
		$sql = "SELECT * FROM {$tableName} WHERE id={$id}";
		$obj = ReserveModuleHelper::queryObject($sql);
		$obj["timeline"] = CommonUtil::formatDate($obj["timeline"]);
		$field_list = ReserveModuleFieldHelper::getList("reserve_module_id={$catalog["dynamic_module_id"]} AND is_system=0","order_num DESC");

		$this->assign("obj",$obj);
		$this->assign("field_list",$field_list);
		$this->display();
                
	}
	
	//通过审核
	//用途：POST
	public function docheck(){
		$catalogId = $this->_post("cid");
		$id = $this->_post("id");
		
		$catalog = CatalogHelper::getObject("catalog_id={$catalogId}");
		$reserveModule = ReserveModuleHelper::getObject("reserve_module_id={$catalog["dynamic_module_id"]}");
		$tableName = C("DB_PREFIX").$reserveModule["table_name"];
		
		$sql = "SELECT * FROM {$tableName} WHERE id={$id}";
		$obj = ReserveModuleHelper::queryObject($sql);
		if(empty($obj)){
			echo CommonUtil::ajaxReturn ( Status::$RELOAD_PAGE, "对象不存在，操作失败！" );
			return ;
		}
		
		$sql = "UPDATE {$tableName} SET reserve_status=1 WHERE id={$id}";
		$result = ReserveModuleHelper::updateSQL($sql);
		if($result === false){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
			return ;
		}
		
		echo CommonUtil::ajaxReturn(Status::$SUCCESS, "操作成功！");
	}
	
	//删除
	//用途：GET
	public function delete(){
		
		$catalogId = $this->_get("cid");
		$id = $this->_get("id");
		
		$catalog = CatalogHelper::getObject("catalog_id={$catalogId}");
		$reserveModule = ReserveModuleHelper::getObject("reserve_module_id={$catalog["dynamic_module_id"]}");
		$tableName = C("DB_PREFIX").$reserveModule["table_name"];
		
		$sql = "SELECT * FROM {$tableName} WHERE id={$id}";
		$obj = ReserveModuleHelper::queryObject($sql);
		if(empty($obj)){
			echo CommonUtil::ajaxReturn ( Status::$RELOAD_PAGE, "对象不存在，操作失败！" );
			return ;
		}
		
		$sql = "DELETE FROM {$tableName} WHERE id={$id}";
		$result = ReserveModuleHelper::updateSQL($sql);
		if($result === false){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
			return ;
		}
		
		echo CommonUtil::ajaxReturn(Status::$RELOAD_PAGE, "操作成功！");
		
	}
	

}